package org.pt.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import static org.pt.config.Constants.MAIL_SENDER;

@Service
@RequiredArgsConstructor
@Slf4j
public class MailServiceImpl  {

    private final JavaMailSender mailSender;

    private static final String FROM_EMAIL =MAIL_SENDER;


    @Async("mailTaskExecutor")
    public void sendAsync(String to, String subject, String text) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(FROM_EMAIL);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            mailSender.send(message);

            log.info("邮件发送成功，收件人: {}", to);
        } catch (Exception e) {
            log.error("邮件发送失败，收件人: {}, 错误: {}", to, e.getMessage());
        }
    }
}
